 /**
 * Copyright (c) 2008, Mu Dynamics.
 *  All rights reserved.
 *  
 *   Redistribution and use in source and binary forms, with or without modification, 
 *   are permitted provided that the following conditions are met:
 *   
 *  - Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation and/or 
 *     other materials provided with the distribution.
 *  - Neither the name of the "Mu Dynamics" nor the names of its contributors may be used 
 *     to endorse  or promote products derived from this software without specific prior 
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.mu.jacob.core.renderer;

/**
 * Common tools for strings with main focus
 * on source code generation
 * 
 * @author Adam Smyczek
 */
public class StringTool {

	private static StringTool instance = null;
	
	public static StringTool getInstance() {
		if (instance == null) {
			instance = new StringTool();
		}
		return instance;
	}
	
	public String firstToUpperCase(CharSequence charSequence) {
		return firstToUpperCase(charSequence.toString());
	}
	
	public String firstToUpperCase(String string) {
		if (string != null) {
			String post = string.substring(1, string.length());
			String first = ("" + string.charAt(0)).toUpperCase();
			return first + post;
		}
		return null; 
	}

	public String firstToLowerCase(CharSequence charSequence) {
		return firstToLowerCase(charSequence.toString());
	}
	
	public String firstToLowerCase(String string) {
		if (string != null) {
			String post = string.substring(1, string.length());
			String first = ("" + string.charAt(0)).toLowerCase();
			return first + post;
		}
		return null;
	}

	public String truncateLastS(CharSequence charSequence) {
		return truncateLastS(charSequence.toString());
	}

	public String toUnderScoreNotation(CharSequence charSequence) {
		return toUnderScoreNotation(charSequence.toString());
	}

	public String toSpaceNotation(CharSequence charSequence) {
		return toSpaceNotation(charSequence.toString());
	}
	
	public String getPluralForm(CharSequence charSequence) {
		return getPluralForm(charSequence.toString());
	}

	public String truncateLastS(String string) {
		if (string != null) {
			if (string.substring(string.length() - 1).equalsIgnoreCase("s")) {
				return string.substring(0, string.length() - 1);
			}
			return string;
		}
		return null;
	}
	
	private CharSequence splitCamelCase(String camelCase, CharSequence delimiter) {
		if (camelCase != null) {
			StringBuffer result = new StringBuffer(camelCase.length() + 5);
			char[] camelChars = camelCase.toCharArray();
			for (int i = 0; i < camelChars.length; i++) {
				char c = camelChars[i];
				if ((i < (camelChars.length-1)) &&
					Character.isUpperCase(c) &&
					Character.isLowerCase(camelChars[i+1])) {
					if (result.length() > 0)
						result.append(delimiter);
				}
				result.append(c);
			}
			return result;
		}
		return null;
	}
	
	public String toUnderScoreNotation(String camelNotation) {
		return splitCamelCase(camelNotation, "_").toString().toUpperCase();
	}
	
	public String toSpaceNotation(String camelNotation) {
		return splitCamelCase(camelNotation, " ").toString();
	}
	
	// plural
	public String getPluralForm(String singular) {
		if (singular.endsWith("y")) {
			return (singular.substring(0, singular.length() - 1) + "ies");  
		}
		if (singular.endsWith("x")) {
			return (singular + "es");  
		}
		if (singular.endsWith("ss")) {
			return (singular + "es");  
		}
		return (singular + "s");
	}
	
	public String getSingularForm(String plural) {
		if (plural.endsWith("ies")) { // "bunnies" -> "bunny"
			return (plural.substring(0, plural.length() - 3) + "y");
		}
		if (plural.endsWith("xes")) { // "boxes" -> "box"
			return (plural.substring(0, plural.length() - 2));			
		}
		if (plural.endsWith("sses")) { // "addresses" -> "address"
			return (plural.substring(0, plural.length() - 2));
		}
		if (plural.endsWith("s")) { // "tacos" -> "taco"
			return (plural.substring(0, plural.length() - 1));			
		}
		return plural;
	}

}
